3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
26 * API Module to move pages
29 class ApiMove
extends ApiBase
{
31 public function execute() {
32 $this->useTransactionalTimeLimit();
34 $user = $this->getUser();
35 $params = $this->extractRequestParams();
37 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
39 if ( isset( $params['from'] ) ) {
40 $fromTitle = Title
::newFromText( $params['from'] );
41 if ( !$fromTitle ||
$fromTitle->isExternal() ) {
42 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
44 } elseif ( isset( $params['fromid'] ) ) {
45 $fromTitle = Title
::newFromID( $params['fromid'] );
47 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
51 if ( !$fromTitle->exists() ) {
52 $this->dieWithError( 'apierror-missingtitle' );
54 $fromTalk = $fromTitle->getTalkPage();
56 $toTitle = Title
::newFromText( $params['to'] );
57 if ( !$toTitle ||
$toTitle->isExternal() ) {
58 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
60 $toTalk = $toTitle->getTalkPageIfDefined();
62 if ( $toTitle->getNamespace() == NS_FILE
63 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $toTitle )
64 && MediaWikiServices
::getInstance()->getRepoGroup()->findFile( $toTitle )
66 if ( !$params['ignorewarnings'] && $user->isAllowed( 'reupload-shared' ) ) {
67 $this->dieWithError( 'apierror-fileexists-sharedrepo-perm' );
68 } elseif ( !$user->isAllowed( 'reupload-shared' ) ) {
69 $this->dieWithError( 'apierror-cantoverwrite-sharedfile' );
74 if ( $user->pingLimiter( 'move' ) ) {
75 $this->dieWithError( 'apierror-ratelimited' );
78 // Check if the user is allowed to add the specified changetags
79 if ( $params['tags'] ) {
80 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
81 if ( !$ableToTag->isOK() ) {
82 $this->dieStatus( $ableToTag );
87 $toTitleExists = $toTitle->exists();
88 $status = $this->movePage( $fromTitle, $toTitle, $params['reason'], !$params['noredirect'],
89 $params['tags'] ?
: [] );
90 if ( !$status->isOK() ) {
91 $user->spreadAnyEditBlock();
92 $this->dieStatus( $status );
96 'from' => $fromTitle->getPrefixedText(),
97 'to' => $toTitle->getPrefixedText(),
98 'reason' => $params['reason']
101 // NOTE: we assume that if the old title exists, it's because it was re-created as
102 // a redirect to the new title. This is not safe, but what we did before was
103 // even worse: we just determined whether a redirect should have been created,
104 // and reported that it was created if it should have, without any checks.
105 $r['redirectcreated'] = $fromTitle->exists();
107 $r['moveoverredirect'] = $toTitleExists;
109 // Move the talk page
110 if ( $params['movetalk'] && $toTalk && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
111 $toTalkExists = $toTalk->exists();
112 $status = $this->movePage(
116 !$params['noredirect'],
117 $params['tags'] ?
: []
119 if ( $status->isOK() ) {
120 $r['talkfrom'] = $fromTalk->getPrefixedText();
121 $r['talkto'] = $toTalk->getPrefixedText();
122 $r['talkmoveoverredirect'] = $toTalkExists;
124 // We're not going to dieWithError() on failure, since we already changed something
125 $r['talkmove-errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
129 $result = $this->getResult();
132 if ( $params['movesubpages'] ) {
133 $r['subpages'] = $this->moveSubpages(
137 $params['noredirect'],
138 $params['tags'] ?
: []
140 ApiResult
::setIndexedTagName( $r['subpages'], 'subpage' );
142 if ( $params['movetalk'] ) {
143 $r['subpages-talk'] = $this->moveSubpages(
147 $params['noredirect'],
148 $params['tags'] ?
: []
150 ApiResult
::setIndexedTagName( $r['subpages-talk'], 'subpage' );
154 $watch = 'preferences';
155 if ( isset( $params['watchlist'] ) ) {
156 $watch = $params['watchlist'];
160 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
161 $this->setWatch( $watch, $toTitle, 'watchmoves' );
163 $result->addValue( null, $this->getModuleName(), $r );
169 * @param string $reason
170 * @param bool $createRedirect
171 * @param array $changeTags Applied to the entry in the move log and redirect page revision
174 protected function movePage( Title
$from, Title
$to, $reason, $createRedirect, $changeTags ) {
175 $mp = new MovePage( $from, $to );
176 $valid = $mp->isValidMove();
177 if ( !$valid->isOK() ) {
181 $user = $this->getUser();
182 $permStatus = $mp->checkPermissions( $user, $reason );
183 if ( !$permStatus->isOK() ) {
187 // Check suppressredirect permission
188 if ( !$user->isAllowed( 'suppressredirect' ) ) {
189 $createRedirect = true;
192 return $mp->move( $user, $reason, $createRedirect, $changeTags );
196 * @param Title $fromTitle
197 * @param Title $toTitle
198 * @param string $reason
199 * @param bool $noredirect
200 * @param array $changeTags Applied to the entry in the move log and redirect page revisions
203 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect, $changeTags = [] ) {
206 $mp = new MovePage( $fromTitle, $toTitle );
208 $mp->moveSubpagesIfAllowed( $this->getUser(), $reason, !$noredirect, $changeTags );
209 if ( !$result->isOk() ) {
210 // This means the whole thing failed
211 return [ 'errors' => $this->getErrorFormatter()->arrayFromStatus( $result ) ];
214 // At least some pages could be moved
215 // Report each of them separately
216 foreach ( $result->getValue() as $oldTitle => $status ) {
217 $r = [ 'from' => $oldTitle ];
218 if ( $status->isOK() ) {
219 $r['to'] = $status->getValue();
221 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
229 public function mustBePosted() {
233 public function isWriteMode() {
237 public function getAllowedParams() {
241 ApiBase
::PARAM_TYPE
=> 'integer'
244 ApiBase
::PARAM_TYPE
=> 'string',
245 ApiBase
::PARAM_REQUIRED
=> true
249 'movesubpages' => false,
250 'noredirect' => false,
252 ApiBase
::PARAM_DFLT
=> 'preferences',
253 ApiBase
::PARAM_TYPE
=> [
260 'ignorewarnings' => false,
262 ApiBase
::PARAM_TYPE
=> 'tags',
263 ApiBase
::PARAM_ISMULTI
=> true,
268 public function needsToken() {
272 protected function getExamplesMessages() {
274 'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
275 'reason=Misspelled%20title&movetalk=&noredirect='
276 => 'apihelp-move-example-move',
280 public function getHelpUrls() {
281 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Move';